<%@page import="qldc.pm.tienan.model.TienAn"%>

<%@page import="qldc.pm.tienan.service.TienAnLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.model.TienAn"%>
<%@page import="qldc.pm.qtht.dmdc.utils.DiaChiUtils"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="qldc.pm.tienan.dto.DanhSachTienAnDTO"%>
<%@page import="qldc.pm.tienan.util.DateUtils"%>
<%@page import="qldc.pm.tienan.dto.DS_HoGiaDinhDTO"%>
<%@page import="qldc.pm.tienan.service.DoiTuongTienAnLocalServiceUtil"%>
<%@page import="qldc.pm.csdl.cd90.service.QuanHeVoiChuHoLocalServiceUtil"%>
<%@page import="qldc.pm.csdl.cd90.model.QuanHeVoiChuHo"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="java.util.List"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.searchcontainer.DSHoGiaDinhSearchTerms"%>
<%@page import="qldc.pm.tienan.searchcontainer.DSHoGiaDinhDisplayTerms"%>
<%@page import="qldc.pm.tienan.searchcontainer.DSHoGiaDinhSearch"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="java.text.SimpleDateFormat"%>

<%@include file="../../init.jsp"%>

<%
	PortletURL portletURL = renderResponse.createRenderURL();
	portletURL.setParameter("mvcPath", "/html/view.jsp");
	portletURL.setParameter("chonChucNangItem",	TienAnConstants.DS_BKTT);
	portletURL.setParameter("loaiBKTK",	TienAnConstants.DS_HO_GIA_DINH);
	
	PortletURL thoatUrl =  renderResponse.createRenderURL();
	thoatUrl.setParameter("mvcPath", "/html/view.jsp");
	thoatUrl.setParameter("chonChucNangItem",TienAnConstants.BAO_CAO_THONG_KE);
	String pathThemeImages = themeDisplay.getPathThemeImages();
%>

<portlet:resourceURL var="loadQuanHuyen" id="loadQuanHuyen">
	<portlet:param name="tinhIdSelected" value="{0}" />
</portlet:resourceURL>

<portlet:resourceURL var="loadPhuongXa" id="loadPhuongXa">
	<portlet:param name="quanHuyenIdSelected" value="{0}" />
</portlet:resourceURL>

<script type="text/javascript">
var NhapNgay = (function() {
	function NhapNgay() {}
	/*
	 * 
	 */
	 NhapNgay.prototype.initCalendars = function() {
		$("#<portlet:namespace/>tuNgayToaAn").datepicker({
			showOn: "both",
			buttonImage: "<%=pathThemeImages%>/common/calendar.png",
				buttonImageOnly : true,
				buttonText : "<liferay-ui:message key="tuNgayPhamToi" />",
				dateFormat : "dd/mm/yy",
				onClose: function( selectedDate ) {
					 $("#<portlet:namespace/>denNgayToaAn").datepicker( "option", "minDate", selectedDate );
					 }
			});
		$("#<portlet:namespace/>denNgayToaAn").datepicker({
			showOn: "both",
			buttonImage: "<%=pathThemeImages%>/common/calendar.png",
				buttonImageOnly : true,
				buttonText : "<liferay-ui:message key="denNgayPhamToi" />",
				dateFormat : "dd/mm/yy",
				onClose: function( selectedDate ) {
					 $("#<portlet:namespace/>tuNgayToaAn").datepicker( "option", "maxDate", selectedDate );
					 }
			});

		};

		return NhapNgay;
	})();

	$(function() {
		var dom = new NhapNgay();

		dom.initCalendars();
	});
</script>

<portlet:actionURL var="inDanhSachHoGiaDinhURL">
	<portlet:param name="<%=TienAnConstants.ACTION_HANDLER_PARAM %>" value="<%=TienAnConstants.REPORT_ACTION %>"/>
	<portlet:param name="<%=Constants.ACTION %>" value="inDanhSachHoGiaDinh"/>
	<portlet:param name="redirect" value="<%=portletURL.toString() %>"/>
</portlet:actionURL>


<div  class="main-Content" >
	<form action="<%=inDanhSachHoGiaDinhURL.toString()%>" id="<portlet:namespace />frm" method="POST" target="_blank">
	<input type="hidden" name="<portlet:namespace />cmd" id="<portlet:namespace />cmd">
		<%
			SimpleDateFormat sdf = new SimpleDateFormat(TienAnConstants.DATE_PATTERN);
			DSHoGiaDinhSearch dsHoGiaDinhSearch = new DSHoGiaDinhSearch(
					renderRequest, portletURL);
			DSHoGiaDinhDisplayTerms displayTerms = (DSHoGiaDinhDisplayTerms) dsHoGiaDinhSearch
					.getDisplayTerms();
			DSHoGiaDinhSearchTerms searchTerms = (DSHoGiaDinhSearchTerms) dsHoGiaDinhSearch
					.getSearchTerms();
			long noiThuongTruTinhIdSelected = searchTerms.getTinhId();
			long noiThuongTruQuanHuyenIdSelected = searchTerms.getQuanHuyenId();
			long noiThuongTruPhuongXaIdSelected = searchTerms.getPhuongXaId();
			int totalRow = DoiTuongTienAnLocalServiceUtil.countDsHoGiaDinhByVungDiaLy_ThoiGianToaAn
					(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(), searchTerms.getTuNgayToaAn(),
							searchTerms.getDenNgayToaAn());
			
			Date tuNgayToaAn =searchTerms.getTuNgayToaAn();
			Date denNgayToaAn = searchTerms.getDenNgayToaAn();

		%>
		<table class="gridnormal" style="padding-top: 10px;padding-left: 10px">			
			<tr>
				<td class="aui-w20"><liferay-ui:message key="vung-dia-ly" /></td>

				<td class="aui-w60" ><select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSHoGiaDinhDisplayTerms.TINH_ID %>" id="<portlet:namespace/>noiThuongTruTinhId"
					onchange="loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString()%>','<%=noiThuongTruQuanHuyenIdSelected%>');">
						<option value="0"><liferay-ui:message key="chon-tinh" /></option>
						<%
							List<Tinh> tinhs = TinhLocalServiceUtil.findAll();

							for (Tinh tinh : tinhs) {
						%>
						<option value="<%=tinh.getTinhId()%>" <%=(noiThuongTruTinhIdSelected == tinh.getTinhId() || tinh.getLaMacDinh()) ? "selected=selected"
						: StringPool.BLANK%>><%=tinh.getTenTinh()%></option>
						<%
							}
						%>
				</select>
				 <select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSHoGiaDinhDisplayTerms.QUAN_HUYEN_ID %>" id="<portlet:namespace/>noiThuongTruQuanHuyenId" onchange="loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString()%>', '<%=noiThuongTruPhuongXaIdSelected%>');">
				 	<option value="0"><liferay-ui:message key="chon-quan-huyen" /></option>
				 
				 </select>

				<select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSHoGiaDinhDisplayTerms.PHUONG_XA_ID %>" id="<portlet:namespace/>noiThuongTruPhuongXaId">
						<option value="0"><liferay-ui:message key="chon-phuong-xa" /></option>				
				</select>
				
				</td>
				<td class="aui-w20"></td>
			</tr>
			<tr>
				<td class="txtlabel aui-w20"><liferay-ui:message key="ngay-toa-an-tu" /></td>
				<td class="aui-w20 ip-datepicker"><input type="text" id="<portlet:namespace/>tuNgayToaAn" name="<portlet:namespace/><%=DSHoGiaDinhDisplayTerms.TU_NGAY_TOA_AN %>" value="<%=Validator.isNotNull(tuNgayToaAn)? sdf.format(tuNgayToaAn) : ""%>" />
				&nbsp;&nbsp;<liferay-ui:message key="den-ngay" />
				&nbsp;&nbsp;<input type="text" id="<portlet:namespace/>denNgayToaAn" name="<portlet:namespace/><%=DSHoGiaDinhDisplayTerms.DEN_NGAY_TOA_AN %>" value="<%=Validator.isNotNull(denNgayToaAn)?sdf.format(denNgayToaAn) : ""%>" />
				</td>
				<td class="aui-w20"></td>
			</tr>
			<tr>
			<td></td>
			<td colspan="3">
			<div align="left">
				<input type=button onclick="timDanhSachHoGiaDinh();"  id="<portlet:namespace/>Tim"  class="btn btn-primary" name="<portlet:namespace/>Tim" value="<liferay-ui:message key="tim-kiem" />" /> 
			
				<input type=button onclick="taoBaoCaoDanhSachHoGiaDinh();" <%=totalRow == 0?"disabled='disabled'":"" %>  id="<portlet:namespace/>BaoCao"  class="btn btn-primary" name="<portlet:namespace/>BaoCao" value="<liferay-ui:message key="xuat-bao-cao" />" /> 
				<input type=button class="btn"  name="<portlet:namespace/>thoat" value="<liferay-ui:message key="tro-ve" />"  onclick="window.location = '<%=thoatUrl.toString() %>';" />
			</div>
			</td>
			
			</tr>
		</table>
		
	
	</form>
	<%
	if (searchTerms.getTinhId() > 0){
	%>
<liferay-ui:search-container  emptyResultsMessage="no-web-content-were-found"  searchContainer="<%=dsHoGiaDinhSearch %>">
	<liferay-ui:search-container-results>
		<%
		
			results = DoiTuongTienAnLocalServiceUtil.findDsHoGiaDinhByVungDiaLy_ThoiGianToaAn
					(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(), searchTerms.getTuNgayToaAn(),
							searchTerms.getDenNgayToaAn(), searchContainer.getStart(),
							searchContainer.getEnd(), searchContainer.getOrderByComparator());

			total = totalRow;
			
		
			pageContext.setAttribute("results", results);
			pageContext.setAttribute("total", total);
		%>
	</liferay-ui:search-container-results>
	
	<%
			int start = dsHoGiaDinhSearch.getDelta() * (dsHoGiaDinhSearch.getCur() - 1);
					
		%>
	
	<liferay-ui:search-container-row className="qldc.pm.tienan.dto.HoGiaDinhDTO" keyProperty="soSoHoKhau" modelVar="hoGiaDinh">
	<%
	String soSoHoKhau = hoGiaDinh.getSoSoHoKhau();

	List<TienAn> tienAnDangThiHanhAns = DoiTuongTienAnLocalServiceUtil.findTienAnBySoSoHoKhau(soSoHoKhau);
	String ngayTienAn = "";
	for (int i=0;i< tienAnDangThiHanhAns.size();i++){
		String ngayToaAn = Validator.isNotNull(tienAnDangThiHanhAns.get(i).getNgayToaAn())? sdf.format(tienAnDangThiHanhAns.get(i).getNgayToaAn()) :"";
		ngayTienAn+=ngayToaAn;
		if (i < tienAnDangThiHanhAns.size() - 1){
			ngayTienAn+=", ";
		}
	}

	%>
		<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center" name="stt" value="<%=String.valueOf(++start) %>" />
			
			<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center" name="stt" value="<%=String.valueOf(++start) %>" />
		<liferay-ui:search-container-column-text name="ho-va-ten-chu-ho" property="hoVaTenChuHo" />
		<liferay-ui:search-container-column-text cssClass="txt-center" name="so-so-ho-khau" property="soSoHoKhau" />
		<liferay-ui:search-container-column-text name="dia-chi-thuong-tru" property="diaChiThuongTru" />
		<liferay-ui:search-container-column-text name="ngay-tien-an" ><%=ngayTienAn %></liferay-ui:search-container-column-text>
		<liferay-ui:search-container-column-text cssClass="txt-center" name="so-luong-nk-pham-toi" property="soLuongNguoiPhamToi" />
	</liferay-ui:search-container-row>

	<liferay-ui:search-iterator searchContainer="<%=dsHoGiaDinhSearch %>" />
</liferay-ui:search-container>
<%

	} else {
		%>
		<h5><liferay-ui:message key="vui-long-chon-tieu-chi-lap-bao-cao" /></h5>
	<%}
%>
	</div>
<script type="text/javascript">
$(function() {
	 loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString() %>','<%=noiThuongTruQuanHuyenIdSelected %>');
	 loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString() %>', '<%=noiThuongTruPhuongXaIdSelected %>');
	
});


function timDanhSachHoGiaDinh(){
		$('#<portlet:namespace/>frm').removeAttr("target");
		$('#<portlet:namespace/>frm').removeAttr("action");
		$('#<portlet:namespace/>cmd').val('<%=TienAnConstants.TIM%>');
		$("#<portlet:namespace/>frm").attr('onsubmit', 'return true;');
		$("#<portlet:namespace/>frm").submit(); 
		
};
function taoBaoCaoDanhSachHoGiaDinh(){

		 $('#<portlet:namespace/>cmd').val('<%=TienAnConstants.IN%>');
			$("#<portlet:namespace/>frm").attr('target', '_blank');
			$("#<portlet:namespace/>frm").submit();
	 
	
};
</script>	


